import { IPSApplication } from '../../app/ipsapplication';
import { IPSAppDynaDashboardUtil } from '../../app/util/ipsapp-dyna-dashboard-util';
import { PSAjaxControlContainerImpl } from '../psajax-control-container-impl';
import { IPSDBPortletPart } from './ipsdbportlet-part';
import { IPSDashboard } from './ipsdashboard';
import { IPSLayout } from '../layout/ipslayout';

export class PSDashboardImpl extends PSAjaxControlContainerImpl implements IPSDashboard {
  get codeName(): string {
    return this.M.codeName;
  }

  protected psappdynadashboardutil: IPSAppDynaDashboardUtil | null = null;

  getPSAppDynaDashboardUtil(): IPSAppDynaDashboardUtil | null {
    if (this.psappdynadashboardutil != null) return this.psappdynadashboardutil;
    const value = this.M.getPSAppDynaDashboardUtil;
    if (value == null) {
      return null;
    }
    const ipsapplication = this.getParentPSModelObject('app.IPSApplication') as IPSApplication;
    if (ipsapplication != null) {
      this.psappdynadashboardutil = ipsapplication.findPSAppUtil(value) as IPSAppDynaDashboardUtil;
    }
    return this.psappdynadashboardutil;
  }

  get psAppDynaDashboardUtil(): IPSAppDynaDashboardUtil | null {
    return this.getPSAppDynaDashboardUtil();
  }

  getPSAppDynaDashboardUtilMust(): IPSAppDynaDashboardUtil {
    const value = this.getPSAppDynaDashboardUtil();
    if (value == null) {
      throw new Error('未指定应用动态看板功能');
    }
    return value;
  }

  protected pslayout: IPSLayout | null = null;

  getPSLayout(): IPSLayout | null {
    if (this.pslayout != null) return this.pslayout;
    const value = this.M.getPSLayout;
    if (value == null) {
      return null;
    }
    this.pslayout = this.getPSModel4('control.layout.IPSLayout', value, 'getPSLayout') as IPSLayout;
    return this.pslayout;
  }

  get psLayout(): IPSLayout | null {
    return this.getPSLayout();
  }

  getPSLayoutMust(): IPSLayout {
    const value = this.getPSLayout();
    if (value == null) {
      throw new Error('未指定看板布局');
    }
    return value;
  }

  get enableCustomized(): boolean {
    return this.M.enableCustomized;
  }

  get cls(): string {
    return 'PSDashboardImpl';
  }

  instanceof(cls: string): boolean {
    if (cls == 'control.dashboard.IPSDashboard' || cls == 'control.dashboard.IPSDashboardContainer' || cls == 'control.layout.IPSLayoutContainer') return true;
    return super.instanceof(cls);
  }
}
